<template>
  <div style="width: 50%;margin: 0 auto">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="200px" class="demo-ruleForm">
      <el-form-item label="姓名" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="身份证号">
        <el-input v-model="ruleForm.phone"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="ruleForm.telephone"></el-input>
      </el-form-item>
      <el-form-item label="注射地区">
        <div class="chooseProvince">
          <el-select style="margin-left: 10px" v-model="ruleForm.province" placeholder="请选择省份">
            <el-option
                v-for="value in provinceList"
                :label="value.regionName"
                :value="value.regionName"></el-option>
          </el-select>
          <el-select style="margin-left: 10px" v-model="ruleForm.city" placeholder="请选择市区">
            <el-option
                v-for="value in cityList"
                :label="value.regionName"
                :value="value.regionName"></el-option>
          </el-select>
          <el-select style="margin-left: 10px" v-model="ruleForm.region" placeholder="请选择县/镇">
            <el-option
                v-for="value in regionList"
                :label="value.regionName"
                :value="value.regionName"></el-option>
          </el-select>
        </div>
      </el-form-item>
      <el-form-item label="预约日期" required>
        <el-col :span="11">
          <el-form-item prop="date1">
            <el-date-picker type="date" placeholder="选择日期" v-model="ruleForm.date1"
                            style="width: 100%;"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col class="line" :span="2">-</el-col>
        <el-col :span="11">
          <el-form-item prop="date2">
            <el-time-picker placeholder="选择时间" v-model="ruleForm.date2" style="width: 100%;"></el-time-picker>
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-radio-group v-model="ruleForm.gender">
          <el-radio label="1">男</el-radio>
          <el-radio label="0">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "PersonMsg",
  data() {
    return {
      flag: 'person',
      currentActive: 1,
      ruleForm: {
        name: '',
        phone: '',
        telephone:'',
        province: '',
        city: '',
        region: '',
        date1: '',
        date2: '',
        gender: ''
      },
      rules: {
        name: [
          {required: true, message: '请输入姓名', trigger: 'blur'}
        ],
        phone: [
          {required: true, message: '请输入手机号', trigger: 'blur'}
        ],
        date1: [
          {type: 'date', required: true, message: '请选择日期', trigger: 'change'}
        ],
        date2: [
          {type: 'date', required: true, message: '请选择时间', trigger: 'change'}
        ],
        gender: [
          {required: true, message: '请选择活动资源', trigger: 'change'}
        ]
      },
      provinceList: null,
      cityList: null,
      regionList: null
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert('已提交，点击确定进入下一步!');
          this.$bus.$emit("NextStep", this.flag, this.currentActive, this.ruleForm)
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  watch: {
    "ruleForm.province"(newValue, oldValue) {
      axios({
        url: 'http://localhost:88/api/stock/vaccineStock/getCity',
        method: 'get',
        params: {
          newValue: newValue
        }
      }).then(res => {
        this.cityList = res.data.data
      })
    },
    "ruleForm.city"(newValue, oldValue) {
      axios({
        url: 'http://localhost:88/api/stock/vaccineStock/getRegion',
        method: 'get',
        params: {
          newValue: newValue
        }
      }).then(res => {
        this.regionList = res.data.data
      })
    },
  },
  mounted() {
    axios({
      url: 'http://localhost:88/api/stock/vaccineStock/getProvince',
      method: 'get',
    }).then(res => {
      this.provinceList = res.data.data
    })
  }
}
</script>

<style scoped>
.chooseProvince {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
}
</style>
